package com.ruiya.staging.goods.cas.service;

import com.ruiya.staging.goods.cas.client.DataIOClient;
import com.ruiya.staging.goods.cas.client.LocateIOClient;
import com.ruiya.staging.goods.cas.model.GoodsInfoVO;
import com.ruiya.staging.goods.cas.mapper.MapperVO;
import com.ruiya.staging.goods.cas.util.OkUtils;
import com.ruiya.staging.goods.core.constants.ErrorCodeEnum;
import com.ruiya.staging.goods.core.model.GoodsIdVO;
import com.ruiya.staging.goods.data.grpc.*;
import com.ruiya.staging.goods.locate.grpc.GetGoodsIdRequest;
import com.ruiya.staging.goods.locate.grpc.GetGoodsIdResponse;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsInfoService {
    private static final Logger _log = LoggerFactory.getLogger(GoodsInfoService.class);

    @Autowired
    private DataIOClient dataIOClient;

    @Autowired
    private LocateIOClient locateIOClient;

    public Pair<ErrorCodeEnum, GoodsIdVO> createGoodsInfo(String tid, GoodsInfoVO goodsInfo){
        CreateGoodsInfoRequest request = CreateGoodsInfoRequest.newBuilder()
                .setUri(DataURI.newBuilder()
                        .setTid(tid)
                        .setSharding(goodsInfo.getGoodsId())
                        .build())
                .setGoodsInfo(MapperVO.mapFromGoodsInfoVO(goodsInfo))
                .build();

        CreateGoodsInfoResponse grpcResponse = this.dataIOClient.createGoodsInfo(request);
        if(!OkUtils.isOk(grpcResponse.getStatus())) {
            _log.warn("tid={}, rpcStatus={}, rpcMemo={}", tid, grpcResponse.getStatus().getCode(), grpcResponse.getStatus().getMemo());
            return new ImmutablePair<>(ErrorCodeEnum.DATAIOERROR, null);
        }

        GoodsIdVO goodsIdVO = new GoodsIdVO(grpcResponse.getGoodsId());
        return new ImmutablePair<>(ErrorCodeEnum.OK, goodsIdVO);
    }

    public Pair<ErrorCodeEnum, GoodsInfoVO> getGoodsInfo(String tid, String entId, String goodsId){
        GetGoodsInfoRequest request = GetGoodsInfoRequest.newBuilder()
                .setUri(DataURI.newBuilder()
                        .setTid(tid)
                        .setSharding(goodsId)
                        .build())
                .setGoodsId(goodsId)
                .build();

        GetGoodsInfoResponse grpcResponse = this.dataIOClient.getGoodsInfo(request);
        if(!OkUtils.isOk(grpcResponse.getStatus())){
            _log.warn("tid={}, rpcStatus={}, rpcMemo={}", tid, grpcResponse.getStatus().getCode(), grpcResponse.getStatus().getMemo());
            return new ImmutablePair<>(ErrorCodeEnum.DATAIOERROR, null);
        }

        //TODO:auto mapper
        GoodsInfoVO goodsInfoVO = MapperVO.mapToGoodsInfoVO(grpcResponse.getGoodsInfo());

        return new ImmutablePair<>(ErrorCodeEnum.OK, goodsInfoVO);
    }

    public Pair<ErrorCodeEnum, GoodsIdVO>  getGoodsId(String tid, String entId, String goodsNo){
        GetGoodsIdRequest request = GetGoodsIdRequest.newBuilder()
                .setTid(tid)
                .setEntId(entId)
                .setGoodsNo(goodsNo)
                .build();

        GetGoodsIdResponse grpcResponse = this.locateIOClient.getGoodsId(request);
        if(!OkUtils.isOk(grpcResponse.getStatus())){
            _log.warn("tid={}, rpcStatus={}, rpcMemo={}", tid, grpcResponse.getStatus().getCode(), grpcResponse.getStatus().getMemo());
            return new ImmutablePair<>(ErrorCodeEnum.FailedGetGoodsId, null);
        }

        GoodsIdVO goodsIdVO = new GoodsIdVO(grpcResponse.getGoodsId());
        return new ImmutablePair<>(ErrorCodeEnum.OK, goodsIdVO);
    }



}
